﻿// -------------------------------------------------------------------------
//    @FileName         :    NFWorldPlayerIDHashTable.cpp
//    @Author           :    gaoyi
//    @Date             :   2023-07-10
//    @Email			:    445267987@qq.com
//    @Module           :    NFWorldPlayerIDHashTable.cpp
//
// -------------------------------------------------------------------------

#include "NFPlayerMgr.h"
#include "NFPlayer.h"
#include "NFLogicCommon/NFLogicShmTypeDefines.h"
#include "NFComm/NFPluginModule/NFError.h"
#include <map>

#include "NFPlayerUtil.h"
#include "NFComm/NFCore/NFServerTime.h"

NFPlayerMgr::NFPlayerMgr()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFPlayerMgr::~NFPlayerMgr()
{
}

int NFPlayerMgr::CreateInit()
{
    m_dayTickTimer = SetDayCalender(0, 0, 0, 0);
    m_dayOtherTickTimer = SetDayCalender(0, GAME_RESET_HOUR_EVERYDAY, 0, 0);
    m_weekTickTimer = SetWeekCalender(0, 1, 0, 0, 0);
    m_monthTickTimer = SetMonthCalender(0, 1, 0, 0, 0);
    m_secTickTimer = SetTimer(1000, 0, 0, 0, 0, 0);
    m_fiveSecTickTimer = SetTimer(5000, 0, 0, 0, 0, 0);
    m_minuteTickTimer = SetTimer(60000, 0, 0, 0, 0, 0);
    m_fiveMinuteTickTimer = SetTimer(300000, 0, 0, 0, 0, 0);
    m_hourTickTimer = SetTimer(3600000, 0, 0, 0, 0, 0);
    return 0;
}


int NFPlayerMgr::ResumeInit()
{
    return 0;
}

int NFPlayerMgr::OnTimer(int timeId, int callcount)
{
    if (m_secTickTimer == timeId)
    {
        SecTick();
    }
    else if (m_fiveSecTickTimer == timeId)
    {
        FiveSecTick();
    }
    else if (m_minuteTickTimer == timeId)
    {
        MinuteTick();
    }
    else if (m_fiveMinuteTickTimer == timeId)
    {
        FiveMinuteTick();
    }
    else if (m_hourTickTimer == timeId)
    {
        HourTick();
    }
    else if (m_dayTickTimer == timeId)
    {
        LOG_INFO(0, "time:{}", NFTimeUtility::DateTimeToStr(NF_ADJUST_TIMENOW()));
        DayTick();
    }
    else if (m_dayOtherTickTimer == timeId)
    {
        LOG_INFO(0, "time:{}", NFTimeUtility::DateTimeToStr(NF_ADJUST_TIMENOW()));
        DailyOtherLogicTick();
    }
    else if (m_weekTickTimer == timeId)
    {
        LOG_INFO(0, "time:{}", NFTimeUtility::DateTimeToStr(NF_ADJUST_TIMENOW()));
        WeekTick();
    }
    else if (m_monthTickTimer == timeId)
    {
        LOG_INFO(0, "time:{}", NFTimeUtility::DateTimeToStr(NF_ADJUST_TIMENOW()));
        MonthTick();
    }

    return 0;
}

NFPlayer *NFPlayerMgr::GetPlayer(uint64_t cid)
{
    return NFPlayer::GetObjByHashKey(cid);
}

NFPlayer * NFPlayerMgr::CreatePlayer(uint64_t cid)
{
    NFPlayer *pPlayer = GetPlayer(cid);
    CHECK_EXPR(pPlayer == NULL, NULL, "Create player Failed, player exist, palyerId:{}", cid);

    pPlayer = NFPlayer::CreateObjByHashKey(cid);
    CHECK_EXPR(pPlayer, NULL, "Create Player Obj Failed, playerID:{}", cid);

    pPlayer->SetRoleID(cid);
    int iRet = pPlayer->Init();
    if (iRet != 0)
    {
        NFPlayer::DestroyObj(pPlayer);
        NFLogInfo(NF_LOG_DEFAULT, cid, "Create Player Failed,  playerId:{} Init Failed, iRet:{}", cid, GetErrorStr(iRet));
        return NULL;
    }
    pPlayer->SetEventLogFlag(false);

    NFLogInfo(NF_LOG_DEFAULT, 0, "Create Player Success, playerId:{} globalId:{}", cid,
          pPlayer->GetGlobalId());
    return pPlayer;
}

int NFPlayerMgr::DeletePlayer(NFPlayer *pPlayer)
{
    CHECK_NULL(0, pPlayer);

    NFLogInfo(NF_LOG_DEFAULT, 0, "Delete Player Info, playerID, gloablId:{}", pPlayer->GetRoleID(), pPlayer->GetGlobalId());

    NFPlayer::DestroyObj(pPlayer);

    return 0;
}

int NFPlayerMgr::DayTick()
{
    for (auto iter = NFPlayer::Begin(); iter != NFPlayer::End(); ++iter)
    {
        NFPlayer *pPlayer = &(*iter);
        pPlayer->DayTick();
    }
    return 0;
}

int NFPlayerMgr::DailyOtherLogicTick()
{
    for (auto iter = NFPlayer::Begin(); iter != NFPlayer::End(); ++iter)
    {
        NFPlayer* pPlayer = &(*iter);
        pPlayer->DayOtherTick();
    }
    return 0;
}

int NFPlayerMgr::WeekTick()
{
    for (auto iter = NFPlayer::Begin(); iter != NFPlayer::End(); ++iter)
    {
        NFPlayer *pPlayer = &(*iter);
        pPlayer->WeekTick();
    }
    return 0;
}

int NFPlayerMgr::MonthTick()
{
    for (auto iter = NFPlayer::Begin(); iter != NFPlayer::End(); ++iter)
    {
        NFPlayer *pPlayer = &(*iter);
        pPlayer->MonthTick();
    }
    return 0;
}

int NFPlayerMgr::SecTick()
{
    for (auto iter = NFPlayer::Begin(); iter != NFPlayer::End(); ++iter)
    {
        NFPlayer *pPlayer = &(*iter);
        pPlayer->SecTick();
    }
    return 0;
}

int NFPlayerMgr::FiveSecTick()
{
    for (auto iter = NFPlayer::Begin(); iter != NFPlayer::End(); ++iter)
    {
        NFPlayer *pPlayer = &(*iter);
        pPlayer->FiveSecTick();
    }
    return 0;
}

int NFPlayerMgr::MinuteTick()
{
    for (auto iter = NFPlayer::Begin(); iter != NFPlayer::End(); ++iter)
    {
        NFPlayer *pPlayer = &(*iter);
        pPlayer->MinuteTick();
    }
    return 0;
}

int NFPlayerMgr::FiveMinuteTick()
{
    for (auto iter = NFPlayer::Begin(); iter != NFPlayer::End(); ++iter)
    {
        NFPlayer *pPlayer = &(*iter);
        pPlayer->FiveMinuteTick();
    }
    return 0;
}

int NFPlayerMgr::HourTick()
{
    for (auto iter = NFPlayer::Begin(); iter != NFPlayer::End(); ++iter)
    {
        NFPlayer *pPlayer = &(*iter);
        pPlayer->HourTick();
    }
    return 0;
}

